package com.cc.mall.controller;

import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.cc.mall.feign.OssFeign;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cc.mall.entity.ProductPictureEntity;
import com.cc.mall.service.ProductPictureService;
import com.cc.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2020-12-11 15:11:54
 */
@RestController
@RequestMapping("mall/productpicture")
@Api(tags = "商品详情图片")
public class ProductPictureController {

    @Autowired
    private OssFeign ossFeign;

    @Autowired
    private ProductPictureService productPictureService;

    /**
     * 列表
     */


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		ProductPictureEntity productPicture = productPictureService.getById(id);
        return R.ok().put("productPicture", productPicture);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody ProductPictureEntity productPicture){
		productPictureService.save(productPicture);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody ProductPictureEntity productPicture){
		productPictureService.updateById(productPicture);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		productPictureService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @GetMapping("/getPic/{id}")
    public R getPic(@PathVariable("id") Long id){
        List<ProductPictureEntity> list = productPictureService.list(new QueryWrapper<ProductPictureEntity>().eq("pdid",id));
        List<String> collect = list.stream().map(m -> {
            return m.getPicture();
        }).collect(Collectors.toList());
        return R.ok().put("list",collect).put("picIds",list);
    }
    @PostMapping("/upload/{id}")
    public R upload(MultipartFile file,@PathVariable("id") Long id){
        R upload = ossFeign.upload(file);
        String url = (String) upload.get("url");
        System.out.println(url);
        ProductPictureEntity productPictureEntity = new ProductPictureEntity();
        productPictureEntity.setPdid(id);
        productPictureEntity.setPicture(url);
        productPictureService.save(productPictureEntity);
        return R.ok().put("picId",productPictureEntity.getId());
    }


}
